<template>
  <div class="summary-outline-wrap">
    <div class="summary-outline-btn">
      <div class="outline-btn-edit">
        <span v-show="isEdit" class="mr20" @click="editHandler">编辑</span>
        <p class="mr20" v-show="isEdit">
          <span class="mr20" @click="saveHandler">保存</span>
          <span @click="cancelHandler">取消</span>
        </p>
      </div>
      <span class="export-btn" v-pdf="{ el: 'item-detail-p5', title: '实施计划进度表' }">导出</span>
      <!-- <div class="print" v-print="{ el: `#gf`, popTitle: '定制信息' }">打印</div> -->
    </div>
    <div class="item-detail-p5">
      <div class="summary-outline-title">实施计划进度表</div>
      <div id="gf">
        <div class="summary-outline-content">
          <div class="outline-info">
            <div class="outline-info-title">基本信息</div>
            <ul class="outline-info-list">
              <li>
                <span>项目编号：</span>
                <span>{{ detail.code || "--" }}</span>
              </li>
              <li>
                <span>客户名称：</span>
                <!-- <span>{{ detail.orderModel?.companyName || "--" }}</span> -->
                <span>{{ detail.companyName || "--" }}</span>
              </li>
              <li>
                <span>项目经理：</span>
                <span>{{ detail.userName || "--" }}</span>
              </li>
            </ul>
            <ul class="outline-info-list"></ul>
          </div>
          <div class="summary-outline-title-big">实施计划进度表</div>
          <div class="summary-outline-table" v-for="firstItem in detail.taskModels" :key="firstItem.id">
            <TableContent
              :is-edit="isEdit"
              :title="firstItem.name"
              :table-options="{ data: firstItem.projectTaskModel || [], columns: tableDefault.columns }"
            >
              <template #start="{ scope }">
                <div class="task-table-mu">
                  <span>计划： {{ scope.planStartTime ? dayJs(scope.planStartTime).format("YYYY/MM/DD") : "--" }} </span>
                  <span>实际：{{ scope.startTime ? dayJs(scope.startTime).format("YYYY/MM/DD") : "--" }} </span>
                </div>
              </template>
              <template #end="{ scope }">
                <div class="task-table-mu">
                  <span>计划：{{ scope.planEndTime ? dayJs(scope.planEndTime).format("YYYY/MM/DD") : "--" }}</span>
                  <span>实际：{{ scope.endTime ? dayJs(scope.endTime).format("YYYY/MM/DD") : "--" }}</span>
                </div>
              </template>
              <template #day="{ scope }">
                <div class="task-table-mu">
                  <span>
                    计划：{{
                      scope.planEndTime && scope.planStartTime
                        ? dayJs(scope.planEndTime).diff(scope.planStartTime, "day") + 1
                        : "--"
                    }}
                  </span>
                  <span>
                    实际：{{ scope.endTime && scope.startTime ? dayJs(scope.endTime).diff(scope.startTime, "day") + 1 : "--" }}
                  </span>
                </div>
              </template>
              <template #status="{ scope }">
                {{ getDictItem("taskStatus", scope.status) }}
              </template>
              <template #userName="{ scope }">
                {{ scope.userName || "--" }}
              </template>
              <template #people="{ scope }">
                {{ scope.userName ? 1 : 0 }}
              </template>
              <template #actionOptions="scope">
                <span class="task-table-del" @click="deleteAgv(1, scope.scope)">删除</span>
              </template>
            </TableContent>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref, onMounted, inject } from "vue";
import TableContent from "../components/TableContent/index.vue";
import { TableProps } from "@/components/Table/interface";
import { ElMessage } from "element-plus";
import { useDict } from "@/hooks/useDict";
// import mittBus from "@/utils/mittBus";
import { addProjectBomSaveBatch } from "@/api/modules/project"; // getProjectBomPage addProjectTaskSaveOutcomeInfo
import { useHandleData } from "@/hooks/useHandleData";
import dayJs from "dayjs";
// hook
const { getDictItem } = useDict(["taskStatus"]);

// 变量
defineProps({
  detail: {
    type: Object,
    default: () => {}
  },
  task: {
    type: Object,
    default: () => {}
  }
});
const projectId = inject("projectId");
const isEdit = ref(false);
let tableDefault: TableProps = reactive({
  columns: [
    {
      type: "index",
      title: "序号",
      key: "",
      width: 48
    },
    {
      title: "任务名称",
      key: "name",
      width: "1.5fr"
    },
    {
      title: "开始",
      key: "start",
      width: "2fr"
    },
    {
      title: "结束",
      key: "end",
      width: "2fr"
    },
    {
      title: "工期",
      key: "day",
      width: "1.5fr"
    },
    {
      title: "完成情况",
      key: "status"
    },
    {
      title: "责任人",
      key: "userName"
    },
    {
      title: "实施人数",
      key: "people"
    },
    // {
    //   title: "备注",
    //   key: "remark"
    // },
    {
      title: "操作",
      key: "actionOptions",
      width: 80
    }
  ]
});
let itemList = ref({
  data: [],
  columns: tableDefault.columns
});
// 方法
const init = () => {
  // getProjectBomProjectFn();
};
// 获取全部用户
// const getProjectBomProjectFn = async () => {
//   const { data } = await getProjectBomProject({ projectId });
//   itemList.value.data = data || [];
// };
const editHandler = () => {
  isEdit.value = true;
};
const cancelHandler = () => {
  init();
  isEdit.value = false;
};
const saveHandler = async () => {
  try {
    const itemLists = itemList.value.data.map(item => {
      return {
        code: item.code,
        name: item.name,
        count: item.count,
        unit: item.unit,
        brand: item.brand,
        parameter: item.parameter,
        remark: item.remark,
        typeId: item.typeId,
        projectId
      };
    });
    await addProjectBomSaveBatch(itemLists);
    isEdit.value = false;
    refresh();

    ElMessage.success("保存成功");
  } catch (e) {
    console.log(e);
  }
};
const refresh = () => {};
const deleteAgv = async (id, row) => {
  try {
    await useHandleData(
      () =>
        new Promise(resolve => {
          itemList.value.data.splice(row.serialNumber, 1);
          resolve(true);
        }),
      {},
      "删除"
    );
  } catch (e) {
    console.log(e);
  }
};

onMounted(() => {});
</script>
<style lang="scss" scoped>
.summary-outline-wrap {
  box-sizing: border-box;
  min-height: 100%;
  padding: 20px;
  background: #ffffff;
  p {
    margin: 0;
  }
  i {
    font-style: normal;
  }
  .mr20 {
    margin-right: 20px;
  }
  .mt10 {
    margin-top: 10px;
  }
  .summary-outline-btn {
    display: flex;
    justify-content: flex-end;
    font-size: 16px;
    font-weight: 400;
    color: var(--el-color-primary);
    .outline-btn-edit {
      display: flex;
      height: 21px;
      line-height: 21px;
      span {
        cursor: pointer;
      }
    }
  }
  .summary-outline-title {
    margin-top: 12px;
    margin-bottom: 60px;
    font-size: 26px;
    font-weight: 600;
    color: #333333;
    text-align: center;
  }
  .summary-outline-title-big {
    margin-top: 20px;
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    text-align: left;
  }
  .summary-outline-content {
    margin: 0 $project-padding;
  }
  .summary-outline-table {
    margin-top: 30px;
    overflow: hidden;
    &-content {
      ul,
      li {
        padding: 0;
        margin: 0;
        list-style: none;
      }
    }
    .table-content-list {
      display: grid;
      grid-template-columns: 1fr 1fr;
      li {
        margin-bottom: 20px;
        font-size: 14px;
        font-weight: 400;
        span:first-child {
          margin-right: 10px;
          color: #666666;
        }
        span:last-child {
          color: #333333;
        }
      }
    }
    .task-table-mu {
      display: flex;
      flex-flow: column wrap;
      span {
        min-height: 30px;
        line-height: 30px;
        white-space: wrap;
      }
    }
  }
  .summary-outline-other {
    margin-top: 30px;
  }
  .outline-info {
    ul,
    li {
      padding: 0;
      margin: 0;
      list-style: none;
    }
    &-title {
      margin-bottom: 20px;
      font-size: 16px;
      font-weight: 600;
      color: #333333;
    }
    &-list {
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
      column-gap: 10px;
      margin-bottom: 10px !important;
      li {
        display: flex;
        align-items: center;
        font-size: 14px;
        font-weight: 400;
        span:first-child {
          margin-right: 10px;
          color: #666666;
        }
        span:last-child {
          color: #333333;
        }
      }
      .info-width {
        width: 109px;
        text-align: right;
      }
    }
  }
  .summary-outline-custom {
    margin-top: 30px;
    :deep(.el-input),
    :deep(.el-textarea) {
      .el-input__wrapper {
        cursor: default;
        background: $project-background;
        box-shadow: none;
        .el-input__inner {
          cursor: default;
        }
      }
      .el-textarea__inner {
        cursor: default;
        resize: none;
        background: $project-background;
        box-shadow: none;
      }
    }
    :deep(.custom-title-edit) {
      .el-input__wrapper {
        cursor: default;
        background-color: #ffffff;
        box-shadow: none;
        .el-input__inner {
          cursor: default;
        }
      }
      .el-textarea__inner {
        cursor: default;
        background-color: #ffffff;
        box-shadow: none;
      }
    }
  }
  :deep(.el-select) {
    width: 100% !important;
  }
}
</style>
